package edu.bit.water.service;

import edu.bit.water.dao.TableWaterSavingDao;
import edu.bit.water.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * tableWaterSaving的业务层
 * @author HJS
 *
 */
@Service
public class TableWaterSavingService {

    @Autowired
    private TableWaterSavingDao tableWaterSavingDao;

    /**
     * 查询META_MANAGEMENT_INDICATOR中的记录
     * @return META_MANAGEMENT_INDICATOR中的所有记录
     */
    public List<ManagementIndicator> managementSelect(int last_id){
        List<ManagementIndicator> list = tableWaterSavingDao.managementSelect(last_id);
        return list;
    }

    /**
     * 查询META_TECHNICAL_INDICATOR中的记录
     * @return META_TECHNICAL_INDICATOR中的所有记录
     */
    public List<TechnicalIndicator> technicalSelect(int last_id){
        List<TechnicalIndicator> list = tableWaterSavingDao.technicalSelect(last_id);
        return list;
    }

    /**
     * 更新记录
     * @param managementIndicator 标签记录实体
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> managementUpdate(ManagementIndicator managementIndicator) {
        Map<String, String> modelMap = new HashMap<>();
        try {
            int n = tableWaterSavingDao.managementUpdate(managementIndicator);
            if (n == 1)
                modelMap.put("status", "success");
            else
                modelMap.put("status", "failed");
            return modelMap;
        } catch (DuplicateKeyException e) {
            // TODO: handle exception
            if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "创建时间已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }

    /**
     * 更新记录
     * @param technicalIndicator 标签记录实体
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> technicalUpdate(TechnicalIndicator technicalIndicator) {
        Map<String, String> modelMap = new HashMap<>();
        try {
            int n = tableWaterSavingDao.technicalUpdate(technicalIndicator);
            if (n == 1)
                modelMap.put("status", "success");
            else
                modelMap.put("status", "failed");
            return modelMap;
        } catch (DuplicateKeyException e) {
            // TODO: handle exception
            if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "创建时间已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }

    /**
     * 更新记录
     * @param labelWaterSaving 标签记录实体
     * @return 成功返回1,失败返回0
     */
    public Map<String, String> labelUpdate(LabelWaterSaving labelWaterSaving) {
        Map<String, String> modelMap = new HashMap<>();
        try {
            int n = tableWaterSavingDao.labelUpdate(labelWaterSaving);
            if (n == 1)
                modelMap.put("status", "success");
            else
                modelMap.put("status", "failed");
            return modelMap;
        } catch (DuplicateKeyException e) {
            // TODO: handle exception
            if (e.getMessage().indexOf("DAY_UNIQUE") != -1) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "创建时间已存在，请重新输入");
                return modelMap;
            }
        }
        return modelMap;
    }
    /**
     * 获取管理考核指标注释
     * @param  standardNumber
     * @return 成功返回1,失败返回0
     */
    public List<ManagementNote> getManagementNote(String standardNumber){
        List<ManagementNote> list = tableWaterSavingDao.getManagementNote(standardNumber);
        return list;
    }

    /**
     * 获取技术考核指标注释
     * @param  standardNumber
     * @return 成功返回1,失败返回0
     */
    public List<TechnicalNote> getTechnicalNote(String standardNumber){
        List<TechnicalNote> list = tableWaterSavingDao.getTechnicalNote(standardNumber);
        return list;
    }
}
